/*
 * PngPlus.cpp
 *
 *  Created on: Feb 4, 2013
 *      Author: baneling
 */

#include "PngPlus.h"

using namespace png;

PngPlus::PngPlus(string filename,int height,int width,bool init,bool verbose)
{
	this->verbose=verbose;
	cout<<"PngPlus verbose is: "<<this->verbose<<endl;
	this->filename=filename;
	this->height=height+IMAGERATIO;
	this->width=width+IMAGERATIO;

	if(init)
	{
		cout<<"Creating new image: "<<this->filename<<endl;
		image<gray_pixel> imm(this->width,this->height);
		this->img=imm;
		this->init();
	}
	else
	{
		cout<<"Reading image from file: "<<this->filename<<endl;
		image<gray_pixel> imm(this->filename);
		this->img=imm;
	}
}

PngPlus::~PngPlus() {
	// TODO Auto-generated destructor stub
}

void PngPlus::init()
{
	if(this->verbose)
		cout<<"Initializing image: "<<this->filename<<endl;
	for (size_t y = 0; y < this->img.get_height(); ++y)
	{
		for (size_t x = 0; x < this->img.get_width(); ++x)
		{
			//cout<<"y: "<<y<<" x: "<<x<<endl;
			this->img[y][x]=gray_pixel(BLACK);
		}
	}
	this->save();
}

void PngPlus::save()
{
	if(this->verbose)
		cout<<"saving image: "<<this->filename<<endl;
	this->img.write(this->filename);
}

void PngPlus::setPixel(int  hY,int wX,double value)
{
	size_t y,x;
	y=(HMAX-hY)*IMAGERATIO;
	x=(wX+WMAX)*IMAGERATIO;

	value=((value+1)/2)*255;
	//cout<<"hy: "<<hY<<" wx: "<<wX<<" y: "<<y<<" x: "<<x<<endl;
	if(this->verbose)
		cout<<"Changing pixel(y,x): "<<hY<<wX<<" with value: "<<(int)value<<endl;
	this->changePixel(y,x,(int)value);
	this->save();
}

void PngPlus::changePixel(size_t hY,size_t wX,int value)
{
	for(size_t y=hY;y<hY+IMAGERATIO;++y)
	{
		for(size_t x=wX;x<wX+IMAGERATIO;++x)
		{
			this->img[y][x] = gray_pixel(value);
		}
	}
}
